#include <reg51.h>
#include <string.h>
 
#define uchar unsigned char	
#define uint  unsigned int	
 
sbit LcdRs_P   = P2^5;			      
sbit LcdRw_P   = P2^6;			
sbit LcdEn_P   = P2^7;
sbit Status0   = P3^5;
sbit Status1   = P3^6;
sbit Lock      = P3^7;
uchar count=0;
uchar str[7];
uchar password[7]={'1','2','3','4','5','6'};
 
 
//*********************子函数区*********************//
 
void DelayMs(uint time)
{
	uint i,j;
	for(i=0;i<time;i++)
		for(j=0;j<112;j++);
}
 
void LcdWriteCmd(uchar cmd)
{ 
	LcdRs_P = 0;  //选择写指令
	LcdRw_P = 0; 	//选择写
	LcdEn_P = 0;  //E使能拉低
	P0=cmd;  //把指令送入P0
	DelayMs(2); //延时一小会儿，让1602准备接收数据
	LcdEn_P = 1;  //使能线电平变化上升沿，命令送入1602的8位数据口
	DelayMs(2);//延时，让数据发送
	LcdEn_P = 0;	//使能线拉低
}
 
void LcdWriteData(uchar dat)
{
	LcdRs_P = 1;  //选择写数据
	LcdRw_P = 0; //选择写
	LcdEn_P = 0; //E使能拉低
	P0=dat;   //把数据送入P0
	DelayMs(2); //延时一小会儿，让1602准备接收数据
	LcdEn_P = 1;  //使能线电平变化上升沿，命令送入1602的8位数据口    
	DelayMs(2);//延时，让数据发送
	LcdEn_P = 0;//使能线拉低
}
 
 
void LcdInit()
{
	LcdWriteCmd(0x38);        // 16*2显示，5*7点阵，8位数据口   0011 1000
	LcdWriteCmd(0x0F);        // 开显示，显示光标闪烁           0000 1111
	LcdWriteCmd(0x06);        // 地址加1，当写入数据后光标右移  0000 0110
	LcdWriteCmd(0x01);        // 清屏
}
 
void LcdGotoXY(uchar line,uchar column)
{
	if(line==0)        
		LcdWriteCmd(0x80+column); 
	if(line==1)        
		LcdWriteCmd(0x80+0x40+column); 
}
 
void LcdPrintStr(uchar *str)
{
	while(*str!='\0')
			LcdWriteData(*str++);
}
 
 
void LcdShowInit()
{
	LcdGotoXY(0,0);	    							
	LcdPrintStr("Enter Password:");
  LcdGotoXY(1,0);
}
 
void LcdShowInit0()
{
	LcdGotoXY(0,0);	    							
	LcdPrintStr("New Password:");
  LcdGotoXY(1,0);
}
 
void LcdShowCorrect()
{
	LcdGotoXY(0,0);	    							
	LcdPrintStr("====Password====");
	LcdGotoXY(1,0);
	LcdPrintStr("====Correct====");
}
 
void LcdShowError()
{
	LcdGotoXY(0,0);	    							
	LcdPrintStr("====Password====");
	LcdGotoXY(1,0);
	LcdPrintStr("=====Error=====");
}
 
void LcdModifing()
{
	LcdGotoXY(0,0);	    							
	LcdPrintStr("====Password====");
	LcdGotoXY(1,0);
	LcdPrintStr("====Modifing====");
}
 
void LcdModified()
{
	LcdGotoXY(0,0);	    							
	LcdPrintStr("==Password has==");
	LcdGotoXY(1,0);
	LcdPrintStr("==been modified=");
}
 
void LcdRetype()
{
	LcdGotoXY(0,0);	    							
	LcdPrintStr("===Please retype");
	LcdGotoXY(1,0);
	LcdPrintStr("Six-digit number");
}
 
//*********************修改密码*********************//
 
void Modifing()
{	
	uchar Old_password[7];
	uchar New_password[7];
	uchar i;
	for(i=0;i<6;i++) Old_password[i]=password[i];
	while(strcmp(Old_password,password)==0)
	{
	P1=0xFE;
	if(P1!=0xFE){
    DelayMs(10);
		if(P1!=0xFE){
 
	switch(P1){
		
		case 0xEE:
			while(P1!=0xFE);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("1");
		  New_password[count]='1';
		  count++;
		  break;
		case 0xDE:
			while(P1!=0xFE);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("2");
		  New_password[count]='2';
		  count++;
		  break;
		case 0xBE:
			while(P1!=0xFE);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("3");
			New_password[count]='3';
		  count++;
		  break;
		case 0x7E:
			while(P1!=0xFE);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("4");
      New_password[count]='4';
		  count++;
		  break;
	}
}
		}
	
	P1=0xFD;
	if(P1!=0xFD){
    DelayMs(10);
		if(P1!=0xFD){
		switch(P1){
		
		case 0xED:
			while(P1!=0xFD);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("5");
		  New_password[count]='5';
		  count++;
		  break;
		case 0xDD:
			while(P1!=0xFD);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("6");
		  New_password[count]='6';
		  count++;
		  break;
		case 0xBD:
			while(P1!=0xFD);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("7");
		  New_password[count]='7';
		  count++;
		  break;
		case 0x7D:
			while(P1!=0xFD);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("8");
		  New_password[count]='8';
		  count++;
		  break;
	}
}
		}
	
	P1=0xFB;
	if(P1!=0xFB){
    DelayMs(10);
		if(P1!=0xFB){
		switch(P1){
		
		case 0xEB:
			while(P1!=0xFB);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("9");
		  New_password[count]='9';
		  count++;
		  break;
		case 0xDB:
			while(P1!=0xFB);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("0");
		  New_password[count]='0';
		  count++;
		  break;
	}
}
		}
 
//*********************功能键区*********************//
		
	P1=0xF7;
	if(P1!=0xF7){
    DelayMs(10);
		if(P1!=0xF7){
		switch(P1){
		
		case 0xE7:         //清空
			while(P1!=0xF7);
      LcdWriteCmd(0x01);
		  LcdShowInit0();
		  for(i=0;i<count;i++) New_password[i]='\0';
		  count=0;
		  break;
		case 0xB7:         //输入
			while(P1!=0xF7);
      LcdWriteCmd(0x01);
			LcdRetype();
			DelayMs(2000);
			LcdWriteCmd(0x01);
			LcdShowInit0();
		  for(i=0;i<count;i++) New_password[i]='\0';
		  count=0;
      break;			
		}
	}
}
	
//*********************限定位数*********************//			
	while(count==6){
		
	P1=0xF7;
	if(P1!=0xF7){
    DelayMs(10);
		if(P1!=0xF7){
		switch(P1){
			
		case 0xE7:        
			while(P1!=0xF7);
      LcdWriteCmd(0x01);
		  LcdShowInit0();
		  for(i=0;i<count;i++) New_password[i]='\0';
		  count=0;
		  break;
		case 0xB7:         
			while(P1!=0xF7);
			LcdWriteCmd(0x01);
			LcdModified();
			DelayMs(2000);
			LcdWriteCmd(0x01);
			LcdShowInit();
			for(i=0;i<count;i++) password[i]=New_password[i];
		  for(i=0;i<count;i++) New_password[i]='\0';
			count=0;
			Status0=1;
			
		}
	}
}
		     }
	
	}
}
 
 
//*********************主函数区*********************//
 
void main()
{
	  uchar i;
		LcdInit();						
	  LcdShowInit();
 
	while(1)
	{
 
//*********************字符键区*********************//
		
	P1=0xFE;
	if(P1!=0xFE){
    DelayMs(10);
		if(P1!=0xFE){
 
	switch(P1){
		
		case 0xEE:
			while(P1!=0xFE);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("1");
		  str[count]='1';
		  count++;
		  break;
		case 0xDE:
			while(P1!=0xFE);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("2");
		 str[count]='2';
		  count++;
		  break;
		case 0xBE:
			while(P1!=0xFE);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("3");
			str[count]='3';
		  count++;
		  break;
		case 0x7E:
			while(P1!=0xFE);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("4");
      str[count]='4';
		  count++;
		  break;
	}
}
		}
	
	P1=0xFD;
	if(P1!=0xFD){
    DelayMs(10);
		if(P1!=0xFD){
		switch(P1){
		
		case 0xED:
			while(P1!=0xFD);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("5");
		  str[count]='5';
		  count++;
		  break;
		case 0xDD:
			while(P1!=0xFD);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("6");
		  str[count]='6';
		  count++;
		  break;
		case 0xBD:
			while(P1!=0xFD);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("7");
		  str[count]='7';
		  count++;
		  break;
		case 0x7D:
			while(P1!=0xFD);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("8");
		  str[count]='8';
		  count++;
		  break;
	}
}
		}
	
	P1=0xFB;
	if(P1!=0xFB){
    DelayMs(10);
		if(P1!=0xFB){
		switch(P1){
		
		case 0xEB:
			while(P1!=0xFB);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("9");
		  str[count]='9';
		  count++;
		  break;
		case 0xDB:
			while(P1!=0xFB);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("0");
		  str[count]='0';
		  count++;
		  break;
		case 0xBB:
			while(P1!=0xFB);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("#");
		  str[count]='#';
		  count++;
		  break;
		case 0x7B:
			while(P1!=0xFB);
			LcdGotoXY(1,count);	  
	    LcdPrintStr("*");
		  str[count]='*';
		  count++;
		  break;
	}
}
		}
 
//*********************功能键区*********************//
		
	P1=0xF7;
	if(P1!=0xF7){
    DelayMs(10);
		if(P1!=0xF7){
		switch(P1){
			
		case 0xE7:         //清空
			while(P1!=0xF7);
      LcdWriteCmd(0x01);
		  LcdShowInit();
		  for(i=0;i<count;i++) str[i]='\0';
		  count=0;
		  break;
		case 0xB7:         //输入
			while(P1!=0xF7);
		  if(strcmp(str,password)==0){        //验证密码
			Status1=0;
			LcdWriteCmd(0x01);
			LcdShowCorrect();
			DelayMs(2000);
			LcdWriteCmd(0x01);
			LcdShowInit();
		  for(i=0;i<count;i++) str[i]='\0';
			count=0;
			}
		  else if(strcmp(str,"###***")==0){    //修改密码
      Status0=0;
			LcdWriteCmd(0x01);
			LcdModifing();
			DelayMs(2000);
			LcdWriteCmd(0x01);
			LcdShowInit0();
		  for(i=0;i<count;i++) str[i]='\0';
			count=0;
			Modifing();
			Status0=1;                           //修改密码退出提示
			}
			
//**************************************************//	
			
			else{
			LcdWriteCmd(0x01);
			LcdShowError();
			DelayMs(2000);
			LcdWriteCmd(0x01);
			LcdShowInit();
			count=0;
		  break;
			}
	}
}
		}
	
//*********************限定位数*********************//			
	while(count==6){
		
	P1=0xF7;
	if(P1!=0xF7){
    DelayMs(10);
		if(P1!=0xF7){
		switch(P1){
			
		case 0xE7:        
			while(P1!=0xF7);
      LcdWriteCmd(0x01);
		  LcdShowInit();
		  for(i=0;i<count;i++) str[i]='\0';
		  count=0;
		  break;
		case 0xB7:         
			while(P1!=0xF7);
		  if(strcmp(str,password)==0){       
			Status1=0;
			LcdWriteCmd(0x01);
			LcdShowCorrect();
			DelayMs(2000);
			LcdWriteCmd(0x01);
			LcdShowInit();
		  for(i=0;i<count;i++) str[i]='\0';
			count=0;
			}
		  else if(strcmp(str,"###***")==0){    
      Status0=0;
			LcdWriteCmd(0x01);
			LcdModifing();
			DelayMs(2000);
			LcdWriteCmd(0x01);
			LcdShowInit0();
		  for(i=0;i<count;i++) str[i]='\0';
			count=0;
			Modifing();
			Status0=1;
			}
		}
	}
}
		     }
			
		
		  
			if(Lock==0)  Status1=1;
	}
}